const Service = require('egg').Service;

class UserService extends Service {
  // 通过用户名获取用户信息
  async getUserByName(username) {
    const { app } = this;
    try {
      const result = await app.mysql.get('users', { username });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }

  // 注册
  async register(params) {
    const { app } = this;
    try {
      const result = await app.mysql.insert('users', params);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }

  // 修改个性签名
  async editUserInfo(data) {
    const { ctx, app } = this;
    try {
      const result = await app.mysql.update('users', {
        ...data,
      }, { id: data.id }); // 筛选出 id 等于 params.id 的用户
      return result;
    } catch (error) {
      return error;
    }
  }
}

module.exports = UserService;
